<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="用户名" prop="name">
        <el-input v-model.trim="formData.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model.trim="formData.account" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model.trim="formData.password"
          type="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.trim="formData.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="roleIds">
        <el-checkbox-group v-model="formData.roleIds">
          <el-checkbox label="8">总教务</el-checkbox>
          <el-checkbox label="9">教务</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { createUser, updateUser } from '@/apis/user'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        formData: {
          name: '',
          account: null,
          password: '',
          phone: '',
          roleIds: [],
        },
        rules: {
          name: [
            { required: true, trigger: 'change', message: '请输入用户名' },
          ],
          account: [
            { required: true, trigger: 'change', message: '请输入账号' },
          ],
          password: [
            { required: true, trigger: 'change', message: '请输入密码' },
          ],
          phone: [
            { required: true, trigger: 'change', message: '请输入手机号' },
          ],
          roleIds: [
            { required: true, trigger: 'change', message: '请选择角色' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          const { id, account, name, password, phone, roleIds } = row
          this.formData = {
            id,
            account,
            name,
            password,
            phone,
            roleIds: roleIds || [],
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.formData = this.$options.data().formData
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let actionFn
            if (this.formData.id) {
              actionFn = updateUser
            } else {
              actionFn = createUser
            }
            await actionFn({
              ...this.formData,
            })

            this.$baseMessage('操作成功', 'success')
            this.$emit('on-success')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
